﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebApp.BaseDataSvc;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using YY.Common;
using System.Web.SessionState;
using System.Data;
using System.Collections;
namespace WebApp
{
    /// <summary>
    /// 返回格式JSON数据
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class JSONHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 数据接口代理
        /// </summary>
        BaseDataSvc.BaseDataClient BaseDataProxy = new BaseDataSvc.BaseDataClient();
        bool addTotalInfo = true;
        bool addKey = true;
        bool addFooter = false;
        bool addColumnsInfo = false;
        bool export = false;//导出 
        DataTable columnsDT = null;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            context.Response.Cache.SetNoStore();
            string method = context.Request["DataType"];
            string jsonStr = string.Empty;
            string footerStr = string.Empty;
            int recordCount = 0;
            int pageCount = 0;
            Hashtable ht = new Hashtable();
            try
            {
                if (!string.IsNullOrEmpty(context.Request["Export"]))
                {
                    export = Boolean.Parse(context.Request["Export"]);
                    context.Response.Clear();
                    context.Response.Buffer = true;
                }

                if (!string.IsNullOrEmpty(context.Request["AddTotalInfo"]))
                    addTotalInfo = Boolean.Parse(context.Request["AddTotalInfo"]);
                if (!string.IsNullOrEmpty(context.Request["AddKey"]))
                    addKey = Boolean.Parse(context.Request["AddKey"]);
                if (!string.IsNullOrEmpty(method))
                {
                    this.BaseDataProxy.Open();
                    switch (method)
                    {
                        case "User":
                            {
                                var datas = this.BaseDataProxy.FindUsers(context.Request.Params["Code"], context.Request.Params["UserName"]);
                                if (datas != null)
                                    recordCount = datas.Count();
                                //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                ht.Add("rows", datas);
                                break;
                            }
                        case "Client":
                            {
                                int pageIndex = 0;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
                                    pageIndex = Convert.ToInt16(context.Request.Params["pageIndex"]);
                                int pageSize = 0;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
                                    pageSize = Convert.ToInt16(context.Request.Params["pageSize"]);
                                var datas = this.BaseDataProxy.FindClients2(out recordCount, out pageCount, context.Request.Params["ShortName"], context.Request.Params["Name"], context.Request.Params["Tel"], context.Request.Params["Address"], context.Request.Params["ContactMan"], pageSize, pageIndex);

                                if (addKey)
                                {
                                    if (addTotalInfo)
                                        ht.Add("total", recordCount);
                                    ht.Add("rows", datas);
                                }
                                else
                                {
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    jsonStr = new JavaScriptSerializer().Serialize(datas);
                                }

                                break;
                            }
                        case "Order":
                            {

                                bool? deleteFlag = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["deleteFlag"]))
                                    deleteFlag = Convert.ToBoolean(context.Request.Params["deleteFlag"]);
                                DateTime? sDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["sDate"]))
                                    sDate = Convert.ToDateTime(context.Request.Params["sDate"]);
                                DateTime? eDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["eDate"]))
                                {
                                    eDate = Convert.ToDateTime(context.Request.Params["eDate"]);
                                    eDate = eDate.Value.Date.AddDays(1).AddSeconds(-1);
                                }
                                int? agent = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["agent"]))
                                    agent = Convert.ToInt32(context.Request.Params["agent"]);
                                int? bizMan = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["bizMan"]))
                                    bizMan = Convert.ToInt32(context.Request.Params["bizMan"]);
                                int? stowageMethod = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["stowageMethod"]))
                                    stowageMethod = Convert.ToInt32(context.Request.Params["stowageMethod"]);
                                bool? isDrawback = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["isDrawback"]))
                                    isDrawback = Convert.ToBoolean(context.Request.Params["isDrawback"]);
                                int pageIndex = 0;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
                                    pageIndex = Convert.ToInt16(context.Request.Params["pageIndex"]);
                                int pageSize = 10;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
                                    pageSize = Convert.ToInt16(context.Request.Params["pageSize"]);
                                string orderStatus = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["orderStatus"]))
                                    orderStatus = Enum.GetName(typeof(EnumCommon.OrderStatus), int.Parse(context.Request.Params["orderStatus"]));
                                string customsStatus = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["customsStatus"]))
                                    customsStatus = Enum.GetName(typeof(EnumCommon.CustomsStatus), int.Parse(context.Request.Params["customsStatus"]));
                                TB_User user = context.Session["User"] as TB_User;


                                var datas = this.BaseDataProxy.FindOrders2(out recordCount, out pageCount, deleteFlag, context.Request.Params["orderCode"], context.Request.Params["cCode"], sDate, eDate, orderStatus, agent, bizMan,
                                    stowageMethod, context.Request.Params["clientName"], isDrawback, customsStatus, context.Request.Params["checkOffCode"], context.Request.Params["caseCode"], user.id, user.role, context.Request.Params["sort"], context.Request.Params["order"], pageSize, pageIndex);

                                //if (datas != null)
                                //jsonStr = new JavaScriptSerializer().Serialize(datas);

                                ht.Add("total", recordCount);
                                ht.Add("rows", datas);

                                break;
                            }
                        case "QFee":
                            {

                                bool? isVerify = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["isVerify"]))
                                    isVerify = Convert.ToBoolean(context.Request.Params["isVerify"]);
                                DateTime? sDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["sDate"]))
                                    sDate = Convert.ToDateTime(context.Request.Params["sDate"]);
                                DateTime? eDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["eDate"]))
                                    eDate = Convert.ToDateTime(context.Request.Params["eDate"]);
                                int? op = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["op"]))
                                    op = Convert.ToInt32(context.Request.Params["op"]);
                                int? bizMan = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["bizMan"]))
                                    bizMan = Convert.ToInt32(context.Request.Params["bizMan"]);
                                int? item = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["item"]))
                                    item = Convert.ToInt32(context.Request.Params["item"]);
                                int? target = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["target"]))
                                    target = Convert.ToInt32(context.Request.Params["target"]);
                                int? feeType = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["feeType"]))
                                    feeType = Convert.ToInt32(context.Request.Params["feeType"]);
                                int pageIndex = 0;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
                                    pageIndex = Convert.ToInt16(context.Request.Params["pageIndex"]);
                                int pageSize = 10;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
                                    pageSize = Convert.ToInt16(context.Request.Params["pageSize"]);
                                string orderStatus = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["orderStatus"]))
                                    orderStatus = Enum.GetName(typeof(EnumCommon.OrderStatus), int.Parse(context.Request.Params["orderStatus"]));
                                string orderCode = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["orderCode"]))
                                    orderCode = context.Request.Params["orderCode"];
                                string feeCode = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["feeCode"]))
                                    feeCode = context.Request.Params["feeCode"];
                                TB_User user = context.Session["User"] as TB_User;

                                var datas = this.BaseDataProxy.FindFees2(out recordCount, out pageCount, orderCode, orderStatus, bizMan, op, sDate, eDate, feeType, feeCode, item, target, isVerify, user.id, user.role, pageSize, pageIndex);
                                //if (datas != null)
                                //    jsonStr = new JavaScriptSerializer().Serialize(datas);
                                ht.Add("total", recordCount);
                                ht.Add("rows", datas);
                                break;
                            }
                        case "EditOrder":
                            {
                                var eOrderDate = Convert.ToDateTime(context.Request.Params["eOrderDate"]);
                                var eBizMan = Convert.ToInt32(context.Request.Params["eBizMan"]);
                                var eAgent = Convert.ToInt32(context.Request.Params["eAgent"]);
                                var eClient = Convert.ToInt32(context.Request.Params["eClient"]);
                                var eInvoiceHeader = context.Request.Params["eClient"];
                                var eContactInfo = context.Request.Params["eContactInfo"];
                                var eCCode = context.Request.Params["eCCode"];
                                var eStartPort = Convert.ToInt32(context.Request.Params["eStartPort"]);
                                var eEndPort = Convert.ToInt32(context.Request.Params["eEndPort"]);
                                var eImptAndExpt = context.Request.Params["eImptAndExpt"];
                                var eCustomsDeadline = Convert.ToDateTime(context.Request.Params["eCustomsDeadline"]);
                                var eStowageMethod = Convert.ToInt32(context.Request.Params["eStowageMethod"]);
                                var eMemo = context.Request.Params["eMemo"];
                                break;
                            }
                        case "OrderStatus":
                            {
                                DataTable datas = WebCommon.EnumToDataTable(typeof(EnumCommon.OrderStatus));
                                jsonStr = WebCommon.DataTableToJson(datas);
                                addTotalInfo = false;
                                //ht.Add("total", recordCount);
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "OptionType":
                            {
                                DataTable datas = WebCommon.EnumToDataTable(typeof(EnumCommon.OptionType));
                                jsonStr = WebCommon.DataTableToJson(datas);
                                addTotalInfo = false;
                                break;
                            }
                        case "Options":
                            {
                                var datas = this.BaseDataProxy.FindOptions(context.Request.Params["OptionType"], null, null);
                                if (datas != null)
                                    recordCount = datas.Count();
                                ht.Add("total", recordCount);
                                ht.Add("rows", datas);
                                break;
                            }
                        case "StowageMethod":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.装柜形式.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "StowageArea":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.提柜堆场.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "ImptAndExpt":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.进出口类型.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "TCAgent":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.拖车行.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "BGAgent":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.报关行.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                //ht.Add("rows", datas);
                                addTotalInfo = false;
                                break;
                            }
                        case "TransferType":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.中转类型.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "Port":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.港口.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "EditOrderInit":
                            {
                                var datas = this.BaseDataProxy.FindOptions(null, null, null);
                                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                                dictionary.Add("StowageMethod", EnumCommon.OptionType.装柜形式.ToString());
                                dictionary.Add("Harbor", EnumCommon.OptionType.港口.ToString());
                                dictionary.Add("Port", EnumCommon.OptionType.码头.ToString());
                                dictionary.Add("ImptAndExpt", EnumCommon.OptionType.进出口类型.ToString());
                                dictionary.Add("StowageArea", EnumCommon.OptionType.提柜堆场.ToString());
                                dictionary.Add("TCAgent", EnumCommon.OptionType.拖车行.ToString());
                                dictionary.Add("BGAgent", EnumCommon.OptionType.报关行.ToString());
                                dictionary.Add("TransferType", EnumCommon.OptionType.中转类型.ToString());
                                foreach (KeyValuePair<string, string> kvp in dictionary)
                                {
                                    var d = this.BaseDataProxy.FindOptions(kvp.Value, null, null);
                                    ht.Add(kvp.Key, d);
                                }
                                var user = this.BaseDataProxy.FindUsers(null, null);
                                ht.Add("User", user);
                                var client = this.BaseDataProxy.FindClients2(out recordCount, out pageCount, null, null, null, null, null, 0, 1);
                                ht.Add("Client", client);
                                addTotalInfo = false;
                                break;
                            }
                        case "CustomsStatus":
                            {
                                DataTable datas = WebCommon.EnumToDataTable(typeof(EnumCommon.CustomsStatus));
                                jsonStr = WebCommon.DataTableToJson(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "WorkFlow":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["OrderId"]))
                                {
                                    var orderId = Convert.ToInt32(context.Request.Params["OrderId"]);
                                    var datas = this.BaseDataProxy.FindWorkflow(orderId);
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    ht.Add("total", recordCount);
                                    //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                    ht.Add("rows", datas);
                                }
                                break;
                            }
                        case "CustomsHX":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["CustomsId"]))
                                {
                                    var customsId = Convert.ToInt32(context.Request.Params["CustomsId"]);
                                    var datas = this.BaseDataProxy.GetCheckOffs(customsId);
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    ht.Add("total", recordCount);
                                    //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                    ht.Add("rows", datas);
                                }
                                break;
                            }
                        case "Cases":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["OrderId"]))
                                {
                                    var orderId = Convert.ToInt32(context.Request.Params["OrderId"]);
                                    var datas = this.BaseDataProxy.GetCases(orderId);
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    ht.Add("total", recordCount);
                                    //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                    ht.Add("rows", datas);
                                }
                                break;
                            }
                        case "Cargos":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["CaseId"]))
                                {
                                    var caseId = Convert.ToInt32(context.Request.Params["CaseId"]);
                                    var datas = this.BaseDataProxy.GetXCargos(caseId);
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    ht.Add("total", recordCount);
                                    //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                    ht.Add("rows", datas);
                                }
                                break;
                            }
                        case "FeeTarget":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.结算对象.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "Account":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.账号.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "Fees":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["OrderId"]))
                                {
                                    var orderId = Convert.ToInt32(context.Request.Params["OrderId"]);
                                    var datas1 = this.BaseDataProxy.GetXFees(orderId);
                                    if (datas1 != null)
                                    {
                                        if (!string.IsNullOrEmpty(context.Request.Params["FeeType"]))
                                        {
                                            var feeType = int.Parse(context.Request.Params["FeeType"]);
                                            var datas = datas1.Where(p => p.feeType == feeType);
                                            recordCount = datas.Count();
                                            //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                            ht.Add("total", recordCount);
                                            ht.Add("rows", datas);
                                        }

                                    }
                                    else
                                    {
                                        //    jsonStr = new JavaScriptSerializer().Serialize(datas1);
                                        ht.Add("total", recordCount);
                                        ht.Add("rows", datas1);
                                    }
                                }
                                break;
                            }
                        case "CountFee":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["SDate"]) & !string.IsNullOrEmpty(context.Request.Params["EDate"]))
                                {
                                    TB_User user = context.Session["User"] as TB_User;
                                    var sDate = Convert.ToDateTime(context.Request.Params["SDate"]);
                                    var eDate = Convert.ToDateTime(context.Request.Params["EDate"]);
                                    int? bizMan = null;
                                    if (!string.IsNullOrEmpty(context.Request.Params["BizMan"]))
                                        bizMan = Convert.ToInt32(context.Request.Params["BizMan"]);
                                    var datas = this.BaseDataProxy.GetCountFee2(sDate, eDate, bizMan, user.id, user.role);
                                    //jsonStr = WebCommon.DataTableToJson(data);

                                    if (datas != null)
                                    {
                                        if (export)
                                        {
                                            datas.Columns.Remove("OrderId");
                                            datas.AcceptChanges();
                                        }

                                        string[] excepts = new string[] { "业务员", "工作单号", "OrderId" };
                                        List<int> sumColList = new List<int>();
                                        for (int i = 0; i < datas.Columns.Count; i++)
                                        {
                                            if (!excepts.Any(p => p == datas.Columns[i].ColumnName))
                                            {
                                                sumColList.Add(i);
                                            }
                                            datas.Columns[i].Caption = datas.Columns[i].ColumnName;
                                        }

                                        if (export)
                                        {
                                            string title = string.Format("费用汇总{0}至{1}", sDate.ToString("yyyyMMdd"), eDate.ToString("yyyyMMdd"));
                                            ExcelHelper.ToExcel(context.Response, datas, title, title, sumColList);
                                        }
                                        else
                                        {
                                            columnsDT = getColumnsTable();
                                            foreach (DataColumn c in datas.Columns)
                                            {
                                                DataRow dr = columnsDT.NewRow();
                                                dr.SetField<string>("field", c.ColumnName);
                                                dr.SetField<string>("title", c.Caption);
                                                dr.SetField<string>("align", "center");
                                                //dr.SetField<int>("width", 100);
                                                columnsDT.Rows.Add(dr);
                                            }
                                            addColumnsInfo = true;

                                            Dictionary<string, decimal> dic = new Dictionary<string, decimal>();
                                            foreach (DataRow row in datas.Rows)
                                            {
                                                foreach (DataColumn c in datas.Columns)
                                                {
                                                    if (!excepts.Any(p => p == c.ColumnName))
                                                    {
                                                        if (dic.Count < datas.Columns.Count - excepts.Length)
                                                            dic.Add(c.ColumnName, row.Field<decimal>(c));
                                                        else
                                                            dic[c.ColumnName] = dic[c.ColumnName] + row.Field<decimal>(c);
                                                    }
                                                }
                                            }
                                            recordCount = datas.Rows.Count;
                                            footerStr += "\"业务员\":\"合计:\"";
                                            foreach (var d in dic)
                                                footerStr += ",\"" + d.Key + "\":\"" + d.Value.ToString("N2") + "\"";
                                            addFooter = true;
                                            jsonStr = "{\"total\":" + recordCount.ToString() + ",\"rows\":" + WebCommon.DataTableToJson(datas) + ",\"columns\":[" + WebCommon.DataTableToJson(columnsDT) + "],\"footer\":[{" + footerStr + "}]}";
                                            //中文字段前端数据列表宽度不会对齐标题栏宽度
                                            jsonStr = jsonStr.Replace("业务员", "ywy").Replace("工作单号", "gzdh").Replace("应收(人民币)", "ysrmb").Replace("应付(人民币)", "yfrmb").Replace("利润(人民币)", "lrrmb").Replace("应收(美元)", "ysmy").Replace("应付(美元)", "yfmy").Replace("利润(美元)", "lrmy");
                                            //ht.Add("total", recordCount);
                                            //ht.Add("rows", datas);
                                            //ht.Add("columns", columnsDT);
                                            //ht.Add("footer", new JavaScriptSerializer().DeserializeObject(footerStr));
                                        }
                                    }
                                }
                                break;
                            }
                        case "FeeType":
                            {
                                DataTable datas = WebCommon.EnumToDataTable(typeof(EnumCommon.FeeType));
                                jsonStr = WebCommon.DataTableToJson(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "SysRoleType":
                            {
                                DataTable datas = WebCommon.EnumToDataTable(typeof(EnumCommon.SysRoleType));
                                jsonStr = WebCommon.DataTableToJson(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "FeeItems":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.费目类型.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "Unit":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.币制单位.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "PackageType":
                            {
                                var datas = this.BaseDataProxy.FindOptions(EnumCommon.OptionType.包装种类.ToString(), null, null);
                                jsonStr = new JavaScriptSerializer().Serialize(datas);
                                addTotalInfo = false;
                                //ht.Add("rows", datas);
                                break;
                            }
                        case "Files":
                            {
                                if (!string.IsNullOrEmpty(context.Request.Params["OrderId"]))
                                {
                                    var orderId = Convert.ToInt32(context.Request.Params["OrderId"]);
                                    var datas = this.BaseDataProxy.GetAttachs(orderId);
                                    //jsonStr = new JavaScriptSerializer().Serialize(datas);
                                    //addTotalInfo = false;
                                    if (datas != null)
                                        recordCount = datas.Count();
                                    ht.Add("total", recordCount);
                                    ht.Add("rows", datas);
                                }
                                break;
                            }
                        case "Balance":
                            {

                                DateTime? sDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["SDate"]))
                                    sDate = Convert.ToDateTime(context.Request.Params["SDate"]);
                                DateTime? eDate = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["EDate"]))
                                    eDate = Convert.ToDateTime(context.Request.Params["EDate"]);
                                int? target = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["Target"]))
                                    target = Convert.ToInt32(context.Request.Params["Target"]);

                                int? balanceType = null;
                                if (!string.IsNullOrEmpty(context.Request.Params["BType"]))
                                    balanceType = Convert.ToInt32(context.Request.Params["BType"]);

                                int pageIndex = 0;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
                                    pageIndex = Convert.ToInt16(context.Request.Params["pageIndex"]);
                                int pageSize = 10;
                                if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
                                    pageSize = Convert.ToInt16(context.Request.Params["pageSize"]);
                                string bank = string.Empty;
                                if (!string.IsNullOrEmpty(context.Request.Params["Bank"]))
                                    bank = context.Request.Params["Bank"];
                                TB_User user = context.Session["User"] as TB_User;
                                var datas = this.BaseDataProxy.FindBalances2(out recordCount, out pageCount, sDate, eDate, target, balanceType, bank, user.id, user.role, pageSize, pageIndex);
                                ht.Add("total", recordCount);
                                ht.Add("rows", datas);
                            }
                            break;
                        default:
                            break;
                    }
                    this.BaseDataProxy.Close();
                }
            }
            catch { }
            //if (addTotalInfo)
            //{
            //    if (recordCount > 0)
            //    {
            //        if (addFooter)
            //            jsonStr = "{\"total\":" + recordCount.ToString() + ",\"rows\":" + jsonStr + ",\"footer\":[" + footerStr + "]}";
            //        else
            //            jsonStr = "{\"total\":" + recordCount.ToString() + ",\"rows\":" + jsonStr + "}";

            //    }
            //    else
            //        jsonStr = "{\"total\":" + recordCount.ToString() + ",\"rows\":[]}";                
            //}
            if (ht.Count > 0)
            {
                if (ht.ContainsKey("rows"))
                    if (ht["rows"] == null)
                        ht["rows"] = new object[] { };
                jsonStr = new JavaScriptSerializer().Serialize(ht);
            }
            if (!export)
                context.Response.Write(jsonStr);
            context.Response.End();
        }

        /// <summary>
        /// 生成列表
        /// </summary>
        /// <returns></returns>
        DataTable getColumnsTable()
        {

            DataTable dt = new DataTable("myTable");

            //field列
            DataColumn columnField = new DataColumn();//创建一列
            columnField.DataType = System.Type.GetType("System.String");//数据类型
            columnField.ColumnName = "field";//列名
            dt.Columns.Add(columnField);//添加到table
            //title列
            DataColumn columnTitle = new DataColumn();
            columnTitle.DataType = System.Type.GetType("System.String");
            columnTitle.ColumnName = "title";
            dt.Columns.Add(columnTitle);
            //align列
            DataColumn columnAlign = new DataColumn();
            columnAlign.DataType = System.Type.GetType("System.String");
            columnAlign.ColumnName = "align";
            dt.Columns.Add(columnAlign);
            //width列
            //DataColumn columnWidth = new DataColumn();
            //columnWidth.DataType = System.Type.GetType("System.Int32");
            //columnWidth.ColumnName = "width";
            //dt.Columns.Add(columnWidth);

            return dt;
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
